package com.example.day6_viewpager.fragment;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.example.day6_viewpager.R;
import com.example.day6_viewpager.adapter.MyPagerAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 轮播图：自动轮播（定时器）+小圆点跟着滑动（rg+rd）
 */
public class OneFragment extends Fragment {
    private ViewPager viewPager;
    private MyPagerAdapter myPagerAdapter;
    private List<Fragment> list;
    private RadioGroup rg;
    private Timer timer;//定时器
    private int position =0;//显示第几个
    private View view;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            viewPager.setCurrentItem(position++);
            if(position == 3){
                timer.cancel();
            }
        }
    };


    public OneFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_one, container, false);
        initPager();
        //viewpager底部改变
        initLstener();
        //自动轮播
        initTimer();
        return view;
    }

    private void initPager() {
        rg = view.findViewById(R.id.group);
        viewPager = view.findViewById(R.id.view_pager);
        list = new ArrayList<>();
        list.add(new OneFragmentOne());
        list.add(new OneFragmentTwo());
        list.add(new OneFragmentThree());
        myPagerAdapter = new MyPagerAdapter(getChildFragmentManager(),list);
        viewPager.setAdapter(myPagerAdapter);
    }

    private void initLstener() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        rg.check(R.id.rb1);
                        break;
                    case 1:
                        rg.check(R.id.rb2);
                        break;
                    case 2:
                        rg.check(R.id.rb3);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(101);

            }
        },0,1000);
    }

}
